package com.example.test.demo.Controller;

import com.example.test.demo.dao.Login;
import com.example.test.demo.dao.Back;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("login")
public class LoginController {
    @PostMapping
    public Back login(@RequestBody Login loginBO) throws IOException {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" +loginBO.getAppid()+"&secret="
                +loginBO.getSecret()+"&js_code="+ loginBO.getCode()+"&grant_type=authorization_code";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();

        if (response.isSuccessful()){
            String body = response.body().string();
            System.out.println(body);
        }
        return new Back();
    }
}
